package com.exploadingelephants.engine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import ch.exploadingelephants.graph.DotColor;
import ch.exploadingelephants.graph.DotString;
import ch.exploadingelephants.graph.MachineGraph;
import ch.exploadingelephants.model.ConveyorBeltItem;
import ch.exploadingelephants.model.ConveyorDirection;
import ch.exploadingelephants.model.EvaluationResult;
import ch.exploadingelephants.model.EvaluationStatus;
import ch.exploadingelephants.model.Level;
import ch.exploadingelephants.model.Machine;
import ch.exploadingelephants.model.RBBifurcationItem;
import ch.exploadingelephants.model.WriterItem;

import com.exploadingelephants.R;
import com.exploadingelephants.views.DotStringView;
import com.exploadingelephants.views.MachineView;

/**
 * @author Sandro De Zanet
 * 
 */
public class MachineActivity extends Activity {
    /**
     * 
     */
    public static final String LEVEL_EXTRA_KEY = "level"; //$NON-NLS-1$
    private MachineView machineView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	int id = 100;

	Intent intent = getIntent();
	final Level level = (Level) intent
		.getSerializableExtra(LEVEL_EXTRA_KEY);

	RelativeLayout layout = new RelativeLayout(this);
	layout.setGravity(Gravity.CENTER_HORIZONTAL);

	LayoutParams params = createParams();
	params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

	TextView titleView = new TextView(this);
	titleView.setText(level.getTitle());
	titleView.setId(id++);
	layout.addView(titleView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, titleView.getId());
	final Machine machine = level.getMachine();
	machineView = new MachineView(this, machine);
	machineView.setId(id++);
	layout.addView(machineView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, machineView.getId());
	TextView descriptionView = new TextView(this);
	descriptionView.setText(level.getDescription());
	descriptionView.setId(id++);
	layout.addView(descriptionView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, descriptionView.getId());
	final TextView resultView = new TextView(this);
	resultView.setText("no result");
	resultView.setId(id++);
	layout.addView(resultView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, resultView.getId());
	final DotStringView dotStringView = new DotStringView(this,
		new DotString());
	dotStringView.setId(id++);
	layout.addView(dotStringView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, dotStringView.getId());
	ImageButton runButton = new ImageButton(this);
	runButton.setId(id++);
	runButton.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		R.drawable.elephant));
	runButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		MachineGraph machineGraph = machineView.getMachineGraph();
		EvaluationResult result = level.evaluate(machineGraph);
		if (EvaluationStatus.OK.equals(result.getStatus())) {
		    resultView.setText("Good Job!");
		    dotStringView.setDotString(new DotString());
		} else {
		    resultView.setText("NO! " + result.getStatus() + " '"
			    + result.getString() + "'");
		    dotStringView.setDotString(result.getErrorString());
		}
	    }
	});
	layout.addView(runButton, params);

	params = createParams();
	params.addRule(RelativeLayout.RIGHT_OF, runButton.getId());
	params.addRule(RelativeLayout.BELOW, dotStringView.getId());
	Button deleteButton = new Button(this);
	deleteButton.setId(id++);
	deleteButton.setText("X");
	deleteButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		machineView.setActiveItem(null);
	    }
	});
	layout.addView(deleteButton, params);

	params = createParams();
	params.addRule(RelativeLayout.RIGHT_OF, deleteButton.getId());
	params.addRule(RelativeLayout.BELOW, dotStringView.getId());
	Button debugButton = new Button(this);
	debugButton.setId(id++);
	debugButton.setText("Debug");
	debugButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		Intent intent = new Intent(MachineActivity.this,
			DebugActivity.class);
		intent.putExtra(MachineActivity.LEVEL_EXTRA_KEY, level);
		MachineActivity.this.startActivity(intent);
	    }
	});
	layout.addView(debugButton, params);

	View lastCb = null;
	View lastRb = null;
	View lastRw = null;
	View lastBw = null;
	for (final ConveyorDirection direction : ConveyorDirection.values()) {
	    // Conveyor belt
	    ImageButton cb = new ImageButton(this);
	    cb.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getResourceIDs().get(0)));
	    cb.setId(id++);
	    RelativeLayout.LayoutParams lp = createParams();
	    if (lastCb != null) {
		lp.addRule(RelativeLayout.RIGHT_OF, lastCb.getId());
		lp.addRule(RelativeLayout.BELOW, deleteButton.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, deleteButton.getId());
	    }
	    lastCb = cb;
	    cb.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new ConveyorBeltItem(direction));
		}
	    });
	    layout.addView(cb, lp);

	    // RedBlue Bifurcation
	    lp = createParams();
	    if (lastRb != null) {
		lp.addRule(RelativeLayout.BELOW, cb.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastRb.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, cb.getId());
	    }

	    ImageButton rb = new ImageButton(this);
	    lastRb = rb;
	    rb.setId(id++);
	    rb.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getRbResourceID()));
	    layout.addView(rb, lp);
	    rb.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new RBBifurcationItem(direction));
		}
	    });

	    // Red write
	    lp = createParams();
	    if (lastRw != null) {
		lp.addRule(RelativeLayout.BELOW, rb.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastRw.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, rb.getId());
	    }

	    ImageButton rw = new ImageButton(this);
	    lastRw = rw;
	    rw.setId(id++);
	    rw.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getRwResourceID()));
	    layout.addView(rw, lp);
	    rw.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new WriterItem(DotColor.RED,
			    direction));
		}
	    });

	    // Blue write
	    lp = createParams();
	    if (lastBw != null) {
		lp.addRule(RelativeLayout.BELOW, rw.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastBw.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, rw.getId());
	    }

	    ImageButton bw = new ImageButton(this);
	    lastBw = bw;
	    bw.setId(id++);
	    bw.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getBwResourceID()));
	    layout.addView(bw, lp);
	    bw.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new WriterItem(DotColor.BLUE,
			    direction));
		}
	    });
	}

	setContentView(layout);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
	super.onDestroy();
    }

    private LayoutParams createParams() {
	return new RelativeLayout.LayoutParams(
		RelativeLayout.LayoutParams.WRAP_CONTENT,
		RelativeLayout.LayoutParams.WRAP_CONTENT);
    }
}
